﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.TD;
using Coldairarrow.Util;
using EFCore.Sharding;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.AOP
{
    /// <summary>
    /// 出库锁盘处理
    /// </summary>
    public class LockOutStorAttribute : BaseAOPAttribute
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public LockOutStorAttribute()
        {
        }

        /// <summary>
        /// 处理数据之前触发
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="BusException"></exception>
        public override async Task Befor(IAOPContext context)
        {
            /*
             * 全部盘点：全部锁盘     1
               仓库盘点：本仓库锁盘   2 
               部分盘点：仓库+物料    3
             */
            var data = context.Arguments[0];
            //获取当前登录人隶属机构
            string uid = data.GetPropertyValue("CreatorId").ToString();
            string storid = data.GetPropertyValue("StorId").IsNullOrEmpty() ? data.GetPropertyValue("StorageId").ToString() : data.GetPropertyValue("StorId").ToString();
            List<TD_OutStorDetail> OutStorDetails = (List<TD_OutStorDetail>)data.GetPropertyValue("OutStorDetails");
            var op = context.ServiceProvider.GetService<IBase_UserBusiness>();
            var uinfo = await op.GetTheDataAsync(uid);
            using (var scop = context.ServiceProvider.CreateScope())
            {
                var db = scop.ServiceProvider.GetService<IDbAccessor>();
                var clist = await db.GetListBySqlAsync<TD_Check>("SELECT * FROM TD_Check WHERE DeptId='" + uinfo.FactoryId + "' AND Status=2");
                if (clist.Count > 0)
                {
                    //全部锁盘
                    if (clist.Any(x => x.Type.Equals("1")))
                    {
                        throw new BusException("当前正在盘点，请稍后执行操作！");
                    }
                    var clist2 = clist.Where(x => x.Type.Equals("2"));
                    //本仓库锁盘
                    if (clist2.Count() > 0)
                    {
                        foreach (var stor in clist2)
                        {
                            if (storid.Equals(stor.StorId))
                            {
                                throw new BusException("当前正在盘点，请稍后执行操作！");
                            }
                        }
                    }
                    //部分盘点
                    StringBuilder sql = new StringBuilder();
                    sql.Append("SELECT ");
                    sql.Append("a.StorId,");
                    sql.Append("b.MaterialId ");
                    sql.Append("FROM ");
                    sql.Append("TD_Check a ");
                    sql.Append("left join TD_CheckMaterial b on a.Id=b.CheckId ");
                    sql.Append("WHERE a.DeptId='" + uinfo.FactoryId + "' ");
                    sql.Append("AND a.Status=2 ");
                    sql.Append("AND a.Type=3 ");
                    var cmlist = await db.GetListBySqlAsync<CheckStorMaterialDTO>(sql.ToString());
                    foreach (var item in OutStorDetails)
                    {
                        foreach (var chk in cmlist)
                        {
                            if (item.StorId.Equals(chk.StorId) && item.MaterialId.Equals(chk.MaterialId))
                            {
                                throw new BusException("当前正在盘点，请稍后执行操作！");
                            }
                        }
                    }
                }
            }
            await Task.CompletedTask;
        }
    }
}
